import { createAlovaMockAdapter } from '@alova/mock'
import GlobalFetch from 'alova/GlobalFetch'

import user from './user'

import type { BaseResponseData } from '@/api/types'

export function mockAdapter() {
  console.log(
    '%cMock Enabled',
    'color: white;font-weight: bold;background-color: #ce679a;padding-inline: 4px;'
  )

  return createAlovaMockAdapter([user], {
    enable: true,
    httpAdapter: GlobalFetch(),
    delay: 500,
    /**
     * @info mock响应默认全部为请求成功，只在data中判断请求结果
     */
    onMockResponse({ body, status, statusText }) {
      return {
        response: new Response(JSON.stringify(body as BaseResponseData<any, string, number>), {
          status: status,
          statusText: statusText
        }),
        headers: new Headers({})
      }
    },
    mockRequestLogger({ isMock, url, method, ...detail }) {
      console.groupCollapsed(
        `%c${isMock ? 'MOCK' : 'REAL'}%c${method} ${url}`,
        'color: #fff;font-weight: bold;background-color: #409EFF;padding-inline: 4px;',
        'color: #409EFF;font-weight: bold;background-color: #E6F7FF;padding-inline: 4px;'
      )
      console.log(detail)
      console.groupEnd()
    }
  })
}
